<?php
// $Id: AreaModule.php 609 2012-12-17 01:23:51Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Module
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

class AreaModule{

    public $data = array();
    public $maps = array();

    protected $source = 'area';

    public function __construct(){
        $this->source = 'area';
    }

    public function getArr(){
        return array(
            'data' => $this->data,
            'maps' => $this->maps,
        );
    }

    public function getArea($areaid){
        $areaid = intval($areaid);
        return isset($this->data[$areaid]) ? $this->data[$areaid] : array();
    }

    public function getParentArea($areaid){
        $areaid = intval($areaid);
        if(isset($this->data[$areaid]['pid'])){
            return $this->getArea($this->data[$areaid]['pid']);
        }
        return array();
    }

    public function getAreaList($pid){
        $pid = intval($pid);
        if($this->existsArea($pid)){
            foreach($this->maps[$pid] as $areaid){
                $area[$areaid] = $this->data[$areaid];
            }
            return $area;
        }else{
            return array();
        }
    }

    public function existsArea($pid){
        return isset($this->maps[$pid]);
    }

    public function getAncestryIds($pid) {
        $pid      = intval($pid);
        $ancestry = array();

        while($pid >0){
            if($parent = $this->getArea($pid)){
                array_unshift($ancestry, $parent['areaid']);
                $pid = $parent['pid'];
            }
        }

        return $ancestry;
    }

    /**
     * 将数据持久化到数据库
     *
     * @return ConfigModule

    public function flush() {
        $namespace = $this->namespace;
        $name = $value = $vtype = '';
        $sql  = Db::buildSql("REPLACE INTO %s (`name`, `namespace`, `value`, `vtype`) VALUES (?, ?, ?, ?)",
                        Db::table($this->source));
        $stmt = Db::prepare($sql);
        $stmt->bind_param('ssss', $name, $namespace, $value, $vtype);
        foreach($this->config as $name => $value){
            if(is_array($value)){
                $value = serialize($value);
                $vtype = 'array';
            }else{
                $vtype = 'string';
            }
            $stmt->execute();
        }
        $this->updateCache();
        return $this;
    }
*/
    /**
     * 从数据库提取数据
     *
     * @return ConfigModule
     */
    public function fetchDb(){
        $this->data = $this->maps = array();
        $sql = Db::buildSql("SELECT * FROM %s WHERE 1 ORDER BY ordnum", Db::table($this->source));
        $res = Db::query($sql);
        while($row = $res->fetch_assoc()){
            $this->data[$row['areaid']] = $row;
            $this->maps[$row['pid']][]  = $row['areaid'];
        }
        return $this;
    }

    /**
     * 从数据库提取数据
     *
     * @return ConfigModule
     */
    public function fetchCache(){
        $cache = Cache::get($this->source);

        $this->data = isset($cache['data']) ? $cache['data'] : array();
        $this->maps = isset($cache['maps']) ? $cache['maps'] : array();
        return $this;
    }

    /**
     * 更新缓存
     *
     * @return ConfigModule
     */
    public function updateCache(){
        Cache::set($this->source, $this->getArr());
        return $this;
    }
}
